<template>
    <div class="app-container">
          <template>
              所有已删除图片列表

    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <!-- <el-input v-model="pictureQuery.username" placeholder="用户名"/> -->
            <el-select v-model="value" filterable placeholder="请选择用户名">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>

      <!-- <el-form-item>
        <el-select v-model="pictureQuery.level" clearable placeholder="讲师头衔">
          <el-option :value="1" label="高级讲师"/>
          <el-option :value="2" label="首席讲师"/>
        </el-select>
      </el-form-item> -->

      <!-- <el-form-item>
        <el-input v-model="pictureQuery.originalFilename" placeholder="原文件名"/>
      </el-form-item> -->

      <el-form-item label="添加时间">
        <el-date-picker
          v-model="pictureQuery.begin"
          type="datetime"
          placeholder="选择开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item>
      <!-- <el-form-item>
        <el-date-picker
          v-model="pictureQuery.end"
          type="datetime"
          placeholder="选择截止时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          default-time="00:00:00"
        />
      </el-form-item> -->

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <el-button  type="warning" icon="el-icon-delete" class="btn-add" @click="batchBack()" >批量恢复</el-button>
        <!-- 表格 -->
    <el-table
      :data="list"
      border
      fit
      @selection-change="handleSelectionChange"
      highlight-current-row>
<el-table-column type="selection"/>
      <el-table-column
        label="序号"
        width="70"
        align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

<!-- todo   picture_back   -->

      <!-- <el-table-column
        label="头像"
        align="center">
<div class="demo-image__placeholder">
  <div class="block">
    <span class="demonstration">默认</span>
    <el-image :src="scope.row.avatar" ></el-image>
  </div>
</div>
</el-table-column> -->



        <el-table-column label="图片预览" width="70" >
                <template slot-scope="scope">
                    <img :src="scope.row.newFilename" width="100%" height="100%" class="avatar"/>
                </template>
            </el-table-column>



      <!-- <el-table-column prop="userId" label="用户名称" width="100" /> -->


      <!-- <el-table-column label="课程状态" width="80">
        <template slot-scope="scope"> -->
            <!-- ===表示匹配类型和值 -->
            <!-- {{findUserNameByUserId(scope.row.userId) }} -->
          <!-- {{ scope.row.userId===2?'已发布':'未发布' }}
        </template>
      </el-table-column> -->
       <el-table-column prop="username" label="用户名称" width="120" />

       <el-table-column prop="originalFilename" label="原文件名称" width="120" />





      <!-- <el-table-column prop="intro" label="资历"  width="200" /> -->

    

      <el-table-column prop="gmtCreate" label="添加时间" width="160"/>

      <!-- <el-table-column prop="sort" label="排序" width="60" /> -->

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <!-- <router-link :to="'/pictureApi/save/'+scope.row.pid">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link> -->
          <el-button type="info" size="mini" icon="el-icon-delete" @click="delBackDataById(scope.row.pid)">已删除恢复</el-button>
        </template>
      </el-table-column>
    </el-table>
     <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </template>
    </div>
</template>
<script>
import userApi from '@/api/dfs/user'
import pictureApi from '@/api/dfs/picture'
export default {
    //写核心代码位置
    data(){
        return {
            list:[],
            page:1,
            limit:20,
            total:0,
            pictureQuery:{},
            multipleSelection: [],
            tableData:[],
            options:[],
            nameList:[],
            value:''
        }
    },
    created(){
        this.getList()
        this.getNames()
        // this.findAllUser()
    },
    methods:{
      getNames(){
          userApi.getUserNames().then(response=>{
            this.nameList = response.data
            console.log(this.nameList)
          }).then(()=>{
            for (let i = 0; i<this.nameList.length; i++){
              let name=this.nameList[i]
              // alert(name)
              let userName = {}
              userName.value=name
              userName.label=name
              // this.options.value=name
              // this.options.label=name
              this.options.push(userName)
            }
            console.log(this.options)
            // alert(this.options)
          })
      },

    //   findUserNameByUserId(id){
    //     		for (var i = 0; i < this.tableData.length; i++) {
    //           if (id==this.tableData[i].UserId)
    //           //console.log("this.tableData[i]")
    //           return this.tableData[i].username; 
		// }
    //   }
    //   ,
      // findAllUser(){//查询user
      //     userApi.findAll().then(response=>{//请求成功
      //     debugger
      //     // this.tableData = response.data
      //     console.log(response.data)
      //     for (var i = 0; i < response.data.length; i++) {
      //       this.tableData.push("userId",response.data[i].UserId)
      //       this.tableData.push("username",response.data[i].username)
      //     }
      //     console.log("tableData"+this.tableData)
      //     console.log(this.tableData)
      //     }).catch(error =>{//请求失败
      //           console.log("请求失败:"+error)
      //       })
      // },

        //列表方法获取删除的数据列
        getList(page = 1){
          this.pictureQuery.username=this.value
            this.page = page
            pictureApi.pageDelList(this.pictureQuery)
            .then(response=>{//请求成功
            // response接口返回的数据
                // console.log(response)
                this.list = response.data.records
                this.total = response.data.total
                //  console.log(this.list)  
                //  console.log(this.total) ;
            })
            .catch(error =>{//请求失败
                console.log("请求失败:"+error)
            })
        }
        ,
        resetData() {//清空的方法
            //表单输入项数据清空
            this.pictureQuery = {}
            this.value=''
            //查询所有讲师数据
            // this.getList(this.page)
        }
        ,
        removeDataById(id){
             this.$confirm('此操作将永久删除图片记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
             pictureApi.removeById(id)
            .then(response=>{//请求成功
                this.$message({
                     type: 'success',
                     message: '删除成功!'
                })
                this.getList()
            })
        })
        },

                // 当多选选项发生变化的时候调用
        handleSelectionChange(selection) {
          console.log(selection)
          this.multipleSelection = selection
        },


                        // 批量恢复
        batchBack() {
          if (this.multipleSelection.length === 0) {
            this.$message.warning('请选择要恢复的记录！')
            return
          }
          // alert(this.multipleSelection[0].pid)
          this.$confirm('此操作将恢复这些记录, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            // 点击确定，远程调用ajax
            // 遍历selection，将id取出放入id列表

           this.idList = []
            this.multipleSelection.forEach(item => {
              // alert(item.pid)
              // alert(item.originalFilename)
              this.idList.push(item.pid)
            })
            // alert("idList:"+this.idList)
            // 调用api.batchRemove
            return pictureApi.batchDelback(this.idList)
          }).then((response) => {
            // this.idList=[]
            this.getList()
            this.$message.success(response.message)
          }).catch(error => {
            if (error === 'cancel') {
              this.$message.info('取消恢复')
            }
          })
        },

        //删除恢复
        delBackDataById(id){
         this.$confirm('此操作将恢复图片记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var pid=[]
          pid.push(id)
             pictureApi.batchDelback(pid)
            .then(response=>{//请求成功
                this.$message({
                     type: 'success',
                     message: '恢复成功!'
                })
                this.getList()
            })
        })
        }
    }
}
</script>